package com.yiwei.zxp.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yiwei.zxp.R;
import com.yiwei.zxp.activity.BuyerGoodDetailsActivity;
import com.yiwei.zxp.activity.BuyerGoodDetailsV2Activity;
import com.yiwei.zxp.api.model.ShopMallListModel;
import com.yiwei.zxp.util.GlideRoundTransform;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by xsooy-pc on 2017/10/23.
 */

public class RankAdapter extends ItemAdapter<ShopMallListModel.DatasBean, RankAdapter.DefaultHolder> {

    private Context context;

    public RankAdapter(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public DefaultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DefaultHolder(inflate(R.layout.item_rank, parent));
    }

    @Override
    public void onBindViewHolder(DefaultHolder holder, int position) {
        ShopMallListModel.DatasBean item = getItem(position);
        holder.txtTitle.setText(item.title);
        holder.txtSell.setText("总销量" + item.sell);
        if (item.is_self == 1) {
            holder.txtSign.setVisibility(View.VISIBLE);
        } else {
            holder.txtSign.setVisibility(View.GONE);
        }
        if (position <= 2) {
            holder.txtRank.setBackgroundResource(R.mipmap.img_sign_red);
        } else {
            holder.txtRank.setBackgroundResource(R.mipmap.img_sign_grey);
        }
        holder.txtRank.setText((position + 1) + "");
        holder.btnAddCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onChooseListener != null) {
                    onChooseListener.onClick(item.shop_id, item.goods_id, item.goods_no);
                }
            }
        });
        holder.imgIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, BuyerGoodDetailsV2Activity.class);
                intent.putExtra("goods_id", item.goods_id);
                intent.putExtra("goods_no", item.goods_no);
                context.startActivity(intent);
            }
        });
        if (item.photos != null && item.photos.size() != 0) {
            Glide.with(context).load(item.photos.get(0)).asBitmap().centerCrop().placeholder(R.color.image_default).transform(new GlideRoundTransform(context, 3)).into(holder.imgIcon);
        }
    }

    class DefaultHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_icon)
        ImageView imgIcon;
        @BindView(R.id.txt_rank)
        TextView txtRank;
        @BindView(R.id.txt_title)
        TextView txtTitle;
        @BindView(R.id.txt_money)
        TextView txtMoney;
        @BindView(R.id.txt_sell)
        TextView txtSell;
        @BindView(R.id.txt_sign)
        TextView txtSign;
        @BindView(R.id.btn_add_cart)
        ImageView btnAddCart;

        public DefaultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    private OnChooseListener onChooseListener;

    public void setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
    }

    public interface OnChooseListener {
        void onClick(String shop_id,
                     String goods_id,
                     String goods_no);
    }
}
